<template>
  <div class="login-container">
    <div class="login-page">
      <el-form :model="ruleForm2" :rules="rules2"
               status-icon
               ref="ruleForm2"
               label-position="left"
               label-width="0px"
               class="demo-ruleForm">
        <h3 class="title">登录</h3>
        <el-form-item prop="username">
          <el-input type="text"
                    v-model="ruleForm2.username"
                    auto-complete="off"
                    placeholder="手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="ruleForm2.password"
                    auto-complete="off"
                    placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
        </el-form-item>
        <div class="footer">
          <router-link to="/app/user/register" class="register">注册</router-link>
          <router-link  to="/app/user/forget" class="forgerPassword">忘记密码</router-link>
        </div>
      </el-form>
    </div>

  </div>
</template>

<script>
    export default {
        name: "login",
      data(){
        return {
          logining: false,
          redirect: undefined,
          ruleForm2: {
            username: '',
            password: '',
          },
          rules2: {
            username: [{required: true, message: '请输入手机号', trigger: 'blur'}],
            password: [{required: true, message: '请输入密码', trigger: 'blur'}]
          },
          checked: false
        }
      },
      methods: {
        handleSubmit(event){
          this.$refs.ruleForm2.validate((valid) => {
            if (valid) {
              this.$store.dispatch('user/login', this.ruleForm2).then(() => {
                this.$router.push({ path: this.redirect || '/app/index' });
              }).catch((response) => {

                console.log(response);
              })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      },
      watch: {
        $route: {
          handler: function(route) {
            this.redirect = route.query && route.query.redirect
          },
          immediate: true
        }
      },
    }
</script>

<style scoped>
  .login-container {
    display: table;
    height: 100%;
    text-align: center;
    width: 100%;
  }
  .login-page {
    display: table-cell;
    vertical-align: middle;
    text-align: center;

  }
  .demo-ruleForm{
    -webkit-border-radius: 5px;
    border-radius: 5px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
  }
  .footer{
    color: #4dd9d5;
    padding-bottom: 20px;
  }
  .register{
    float: left;
  }
  .forgerPassword{
    float: right;
  }
</style>
